package com.loveyukari.dao.impl;

import com.loveyukari.dao.ExchangeGfitDao;
import com.loveyukari.dao.ExchangeHistoryDao;
import com.loveyukari.model.ExchangeGfit;
import com.loveyukari.model.ExchangeHistory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JdbcExchangeGfitDao extends BaseDao implements ExchangeGfitDao {

    private RowMapper<ExchangeGfit> rowMapper = new RowMapper<ExchangeGfit>() {
        @Override
        public ExchangeGfit mapRow(ResultSet resultSet, int i) throws SQLException {
            ExchangeGfit exchangeGfit = new ExchangeGfit();
            exchangeGfit.setId(resultSet.getInt("id"));
            exchangeGfit.setName(resultSet.getString("name"));
            exchangeGfit.setPrice(resultSet.getInt("price"));
            exchangeGfit.setImage(resultSet.getString("image"));
            exchangeGfit.setPrice(resultSet.getInt("price"));
            exchangeGfit.setNum(resultSet.getInt("num"));
            exchangeGfit.setStatus(resultSet.getInt("status"));
            exchangeGfit.setType(resultSet.getInt("type"));
            return exchangeGfit;
        }
    };

    @Override
    public List<ExchangeGfit> selectAll() {
        String sql = "select * from exchange_gfit order by id desc";
        return this.getJdbcTemplate().query(sql,rowMapper);
    }

    @Override
    public List<ExchangeGfit> selectOnline() {
        String sql = "select * from exchange_gfit where status = 1 order by price desc";
        return this.getJdbcTemplate().query(sql,rowMapper);
    }

    @Override
    public ExchangeGfit selectById(int id) {
        String sql = "select * from exchange_gfit where id = ?";
        List<ExchangeGfit> list = this.getJdbcTemplate().query(sql, rowMapper, id);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public void updateStatus(int id, int status) {
        String sql = "update exchange_gfit set status = ? where id = ?";
        this.getJdbcTemplate().update(sql,status,id);
    }

    @Override
    public void update(ExchangeGfit exchangeGfit) {
        String sql = "update exchange_gfit set name = ?, price = ?, image = ?, num = ?, type = ? where id = ?";
        this.getJdbcTemplate().update(sql,exchangeGfit.getName(),exchangeGfit.getPrice(),exchangeGfit.getImage(),exchangeGfit.getNum(),exchangeGfit.getType(),exchangeGfit.getId());
    }

    @Override
    public void insert(ExchangeGfit exchangeGfit) {
        String sql = "insert into exchange_gfit (name,price,image,num) values(?,?,?,?)";
        this.getJdbcTemplate().update(sql,exchangeGfit.getName(),exchangeGfit.getPrice(),exchangeGfit.getImage(),exchangeGfit.getNum());
    }
}
